#!/usr/bin/python

import random
import tkinter
from tkinter import ttk
from tkinter import messagebox
import time
import sys
import os


class ButtonNameIterator:
    def __init__(self, my_start=1, stop=26):
        self.start = my_start
        self.stop = stop
        self.current = my_start

    def __iter__(self):
        return self

    def __next__(self):
        if self.current < self.stop:
            number = self.current
            self.current += 1
            return number
        else:
            raise StopIteration


iteration_num = 0
button_names_list = [num for num in range(1, 26)]
shuffled_object_button_list = []
buttons = {}
button_name_iterator_obj = ButtonNameIterator()
start_time = time.time()


def on_button_press(event):
    button_name = next(button_name_iterator_obj)
    if event.widget.cget('text') == str(button_name):
        buttons[str(button_name)].destroy()
        del buttons[str(button_name)]
    else:
        messagebox.showinfo('Mistake', 'Wrong number.')
        button_name_iterator_obj.current -= 1

    if '25' not in buttons.keys():
        game_over()


def button_generator():
    global iteration_num
    while iteration_num <= 25:
        button_text = button_names_list[iteration_num]
        new_button = ttk.Button(buttons_frame, text=str(button_text))
        new_button.bind('<Button-1>', func=on_button_press)
        shuffled_object_button_list.append(new_button)
        iteration_num += 1
        yield new_button


def build_test():
    random.shuffle(button_names_list)

    row = 0

    for column in range(25):
        if column <= 4:
            next(button_generator()).grid(column=column, row=row)
        elif column in [5, 10, 15, 20]:
            row += 1
            next(button_generator()).grid(column=column - 5 * row, row=row)
        elif row >= 1 and column not in [5, 10, 15, 20]:
            next(button_generator()).grid(column=column - 5 * row, row=row)

    for text in shuffled_object_button_list:
        buttons[text['text']] = text


def build_window():
    mainframe.grid(column=0, row=0)

    buttons_frame.grid(column=0, row=0, columnspan=5)

    start_button.grid(column=1, row=1, sticky='W, E, S, N', columnspan=3)

    main_window.bind('<Control-Shift-C>', quit)

    main_window.mainloop()


def game_over():
    end_time = time.strftime('%M:%S', time.gmtime(time.time() - start_time))
    answer = messagebox.askyesno('Test completed', f'Your time is {end_time}\nDo you want to restart test?',
                                 icon='question')

    if answer:
        python = sys.executable
        os.execl(python, python, *sys.argv)
    else:
        quit()


def start():
    global button_name_iterator_obj
    global start_time
    button_name_iterator_obj = ButtonNameIterator()
    start_button.state(['disabled'])
    build_test()
    start_time = time.time()


main_window = tkinter.Tk()
main_window.resizable(False, False)
mainframe = ttk.Frame(main_window)
buttons_frame = ttk.Frame(mainframe, width='420', height='140', relief='sunken')
start_button = ttk.Button(mainframe, text='Start', command=start)


if '__main__' == __name__:
    build_window()
